﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public static class EventManager
{
    public static List<string> eventName = new List<string>();
    public static Dictionary<string, Delegate> eventDic = new Dictionary<string, Delegate>();
    
    /// <summary>
    /// 校对事件类型是否添加正确，因为事件可以累加，只有相同类型的事件才可以累加
    /// </summary>
    static void ChackEvent(string eventName , Delegate callBack)
    {
        if (!eventDic.ContainsKey(eventName))
        {
            eventDic.Add(eventName,null);
        }

        Delegate d = eventDic[eventName];
        if (d != null && d.GetType() != callBack.GetType())
        {
            throw new Exception("添加失败,值的类型与添加的类型不符");
        }
    }
    /// <summary>
    /// 在移除监听前做的校队 
    /// </summary>
    /// <param name="eventName"></param>
    /// <param name="callBack"></param>
    static void OnRemove(string eventName, Delegate callBack)
    {
        if (eventDic.ContainsKey(eventName))
        {
            Delegate d = eventDic[eventName];
            if (d == null)
            {
                throw new Exception("没有对应事件");
            }
            else if (d.GetType() != callBack.GetType())
            {
                throw new Exception("事件表中没有此类型的事件");
            }
        }
        else
        {
            throw new Exception("没有此事件");
        }
    }
    //添加监听
    public static void AddListener(string eventName,CallBack callBack)
    {
        ChackEvent(eventName, callBack);
        eventDic[eventName] = (CallBack)eventDic[eventName] + callBack;
    }
    public static void AddListener<T>(string eventName, CallBack<T> callBack)
    {
        ChackEvent(eventName, callBack);
        eventDic[eventName] = (CallBack<T>)eventDic[eventName] + callBack;
    }
    public static void AddListener<T,A>(string eventName, CallBack<T,A> callBack)
    {
        ChackEvent(eventName, callBack);
        eventDic[eventName] = (CallBack<T, A>)eventDic[eventName] + callBack;
    } 
    public static void AddListener<T, A, B>(string eventName, CallBack<T, A, B> callBack)
    {
        ChackEvent(eventName, callBack);
        eventDic[eventName] = (CallBack<T, A, B>)eventDic[eventName] + callBack;
    }
    //移除监听
    public static void RemoveListener(string eventName,CallBack callBack)
    {
        OnRemove( eventName, callBack);
        eventDic[eventName] = (CallBack)eventDic[eventName] - callBack;
        if (eventDic[eventName] == null)
        {
            eventDic.Remove(eventName);
        }
    }
    public static void RemoveListener<T>(string eventName, CallBack<T> callBack)
    {
        OnRemove(eventName, callBack);
        eventDic[eventName] = (CallBack<T>)eventDic[eventName] - callBack;
        if (eventDic[eventName] == null)
        {
            eventDic.Remove(eventName);
        }
    }
    public static void RemoveListener<T,A>(string eventName, CallBack<T,A> callBack)
    {
        OnRemove(eventName, callBack);
        eventDic[eventName] = (CallBack<T,A>)eventDic[eventName] - callBack;
        if (eventDic[eventName] == null)
        {
            eventDic.Remove(eventName);
        }
    }
    public static void RemoveListener<T,A,B>(string eventName, CallBack<T, A, B> callBack)
    {
        OnRemove(eventName, callBack);
        eventDic[eventName] = (CallBack<T, A, B>)eventDic[eventName] - callBack;
        if (eventDic[eventName] == null)
        {
            eventDic.Remove(eventName);
        }
    }
    //发送并处理事件
    public static void SendEvent(string eventName)
    {
        Delegate d;
        if (eventDic.TryGetValue(eventName,out d))
        {
            CallBack callBack = (CallBack) d;
            if (callBack != null)
            {
                callBack();
            }
            else
            {
                throw new Exception("委托为空");
            }
        }         
    } 
    public static void SendEvent<T>(string eventName, T t)
    {
        Delegate d;
        if (eventDic.TryGetValue(eventName, out d))
        {
            CallBack<T> callBack = (CallBack<T>)d;
            if (callBack != null)
            {
                callBack(t);
            }
            else
            {
                throw new Exception("委托为空");
            }
        }
    }
    public static void SendEvent<T,A>(string eventName, T t,A a)
    {
        Delegate d;
        if (eventDic.TryGetValue(eventName, out d))
        {
            CallBack<T,A> callBack = (CallBack<T,A>)d;
            if (callBack != null)
            {
                callBack(t,a);
            }
            else
            {
                throw new Exception("委托为空");
            }
        }
    }
    public static void SendEvent<T, A,B>(string eventName, T t, A a,B b)
    {
        Delegate d;
        if (eventDic.TryGetValue(eventName, out d))
        {
            CallBack<T, A,B> callBack = (CallBack<T, A,B>)d;
            if (callBack != null)
            {
                callBack(t, a,b);
            }
            else
            {
                throw new Exception("委托为空");
            }
        }
    }
}
